package com.smartHire.controller.Business;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Shop;

import com.smartHire.service.ShopService;

import com.smartHire.util.HttpRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name="ShopRegisterServlet",value = "/ShopRegisterServlet")
public class ShopRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");

        String code = request.getParameter("code");
        String appid = "wxd272773014bab017";
        String secret = "046195cc091413a9edfea2eb463039ab";
        String js_code = code;
        String grant_type = "authorization_code";

        String params = "appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=" + grant_type;
        //发送请求
        String sr = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
        //解析相应内容（转换成json对象）
        JSONObject json = JSONObject.parseObject(sr);
        //获取会话密钥（session_key）
        String session_key = json.get("session_key").toString();
        //用户的唯一标识（openid）
        String openid = (String) json.get("openid");
        System.out.println("openid:" + openid);



        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        try {

            Response R=new Response();
            R.Openid=openid;
            //获取service
            ShopService shopService=(ShopService)ctx.getBean("shopService");

            Shop shop=new Shop();

            shop.setShop_id(openid);

            ArrayList arrayList=new ArrayList();
            arrayList.add(R);

            Shop shop1=shopService.queryShopById(shop);

            System.out.println(shop1);

            if (shop1==null)
            {
                String shop_name=request.getParameter("shop_name");
                String address=request.getParameter("address_name");
                if(shop_name.equals("undefined")){
                    R.Ok="register";
                    response.getWriter().write(JSONObject.toJSONString(arrayList));
                }
                else{
                    shop.setShop_name(shop_name);
                    shop.setShop_address(address);
                    if(shopService.queryShopByName(shop)==null){
                        if (shopService.addShop(shop) == 1) {
                            R.Ok = "ok";
                            arrayList.add(shop);
                            response.getWriter().write(JSONObject.toJSONString(arrayList));
                        } else {
                            R.Ok = "error";
                            response.getWriter().write(JSONObject.toJSONString(arrayList));
                        }
                    }
                    else{
                        R.Ok="cz";
                        response.getWriter().write(JSONObject.toJSONString(arrayList));
                    }

                }
            }
            else if(shop1!=null)
            {
                R.Ok="no1";

                arrayList.add(shop1);

                response.getWriter().write(JSONObject.toJSONString(arrayList));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



    }
}

class Response{
    public String Ok;
    public String Openid;
}
